package com.thciwei.mailserver.receiver;

import com.rabbitmq.client.Channel;
import com.thciwei.proj.bean.Employee;
import com.thciwei.proj.bean.MailConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.Date;


@Component
public class MailReceiver  {
    public static final Logger logger = LoggerFactory.getLogger(MailReceiver.class);
    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    MailProperties mailProperties;
    @Autowired
    TemplateEngine templateEngine;
    @Autowired
    StringRedisTemplate redisTemplate;

    /**
     * redis保证消费可靠性
     */
    @RabbitListener(queues = MailConstants.MAIL_QUEUE_NAME)
    public void handler(Message message, Channel channel) throws IOException {
        Employee employee=(Employee)message.getPayload();
        MessageHeaders headers=message.getHeaders();
        Long tag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        String msgId = (String) headers.get("spring_returned_message_correlation");
        if(redisTemplate.opsForHash().entries("mail_log").containsKey(msgId)){
            //redis包含key，说明消息已经被消费过
            logger.info(msgId+":消息已经被消费");
            //确认消息被消费
            channel.basicAck(tag,false);
            return;
        }
        logger.info(employee.toString());
        //收到消息，发送邮件
        MimeMessage msg = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg);
        try {
            helper.setTo(employee.getEmail());
            helper.setFrom(mailProperties.getUsername());
            helper.setSubject("入职欢迎");
            helper.setSentDate(new Date());
            Context context = new Context();
            context.setVariable("name", employee.getName());
            context.setVariable("posName", employee.getPosition().getName());
            context.setVariable("joblevelName", employee.getJobLevel().getName());
            context.setVariable("departmentName", employee.getDepartment().getName());
            String mail = templateEngine.process("mail", context);
            helper.setText(mail, true);
            javaMailSender.send(msg);
            redisTemplate.opsForHash().put("mail_log",msgId,"thciwei");
            channel.basicAck(tag,false);
            logger.info(msgId+"邮件发送成功");
        } catch (MessagingException e) {
            //发送失败丢弃 true
            channel.basicNack(tag,false,true);
            e.printStackTrace();
            logger.error("邮件发送失败:" + e.getMessage());
        }

    }

/**
 * 不使用 redis
 */

    // @RabbitListener(queues = "thciwei.mail.welcome")

//    @RabbitListener(queues = MailConstants.MAIL_QUEUE_NAME)
//    public void handler(Employee employee) {
//        logger.info(employee.toString());
//        //收到消息，发送邮件
//        MimeMessage message = javaMailSender.createMimeMessage();
//        MimeMessageHelper helper = new MimeMessageHelper(message);
//        try {
//            helper.setTo(employee.getEmail());
//            helper.setFrom(mailProperties.getUsername());
//            helper.setSubject("入职欢迎");
//            helper.setSentDate(new Date());
//            Context context = new Context();
//            context.setVariable("name", employee.getName());
//            context.setVariable("posName", employee.getPosition().getName());
//            context.setVariable("joblevelName", employee.getJobLevel().getName());
//            context.setVariable("departmentName", employee.getDepartment().getName());
//            String mail = templateEngine.process("mail", context);
//            helper.setText(mail, true);
//            javaMailSender.send(message);
//        } catch (MessagingException e) {
//            e.printStackTrace();
//            logger.error("邮件发送失败:" + e.getMessage());
//        }
//
//    }
}
